Uec3.ReceiptAddressAddWindow = Ext.extend(Ext.app.Module, {
	id : 'receiptAddressAdd',
	type : 'common/receiptAddressAdd',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {

		Ext.util.Format.comboRenderer = function(combo) {
			return function(value) {
				var record = combo.findRecord(combo.valueField, value);
				return record
						? record.get(combo.displayField)
						: combo.valueNotFoundText;
			}
		};
	},

	getComponents : function(scope) {

		var receiptAddress_dist_cmb;
		var receiptAddress_city_cmb;
		var receiptAddress_prov_cmb;
		var receiptAddress_win;
		var receiptAddress_form;
		var receiptAddress_ajax;

		getGridComboBox = function() {

			// 区县下拉框
			receiptAddress_dist_cmb = getDistrictCombox(Uec3.App.Constants.homeUrl);
			receiptAddress_dist_cmb.setWidth(162);
			// 城市下拉框
			receiptAddress_city_cmb = getCityCombox(Uec3.App.Constants.homeUrl,
					receiptAddress_dist_cmb);
			receiptAddress_city_cmb.setWidth(162);
			receiptAddress_city_cmb.blankText="收获地址城市必须填写";
			receiptAddress_city_cmb.allowBlank=false;
			// 省份下拉框
			receiptAddress_prov_cmb = getProvinceCombox(
					Uec3.App.Constants.homeUrl, receiptAddress_city_cmb,
					receiptAddress_dist_cmb);
			receiptAddress_prov_cmb.setWidth(162);
			receiptAddress_prov_cmb.blankText="收获地址省份必须填写";
			receiptAddress_prov_cmb.allowBlank=false;

		};

		var receiptAddresscomponentInits = function() {

			// 得到下拉菜单列表
			getGridComboBox();

			receiptAddress_ajax = new Ext.data.Connection();
			// 初始化表单对象
			receiptAddress_form = new Ext.form.FormPanel({
				labelAlign : 'right',
				labelWidth : 60,
				// title:客户档案,
				frame : true,
				width : 350,
				height : 120,
				url : '',
				defaultType : 'textfield',
				layout : 'uec3grid',
				layoutConfig : {
					// hGrid:[8,25,8,25,8,25],
					// vGrid:[33,33,33]
					hGrid : [9, 18, 6, 18, 6, 18, 7, 18],
					vGrid : [33, 33, 33]

				},
				items : [{
							name : 'id',
							fieldLabel : 'ID',
							xtype : 'hidden'
						}, {
							name : 'companyId',
							fieldLabel : '公司ID',
							xtype : 'hidden'
						}, {
							name : 'areaId',
							fieldLabel : '地区ID',
							xtype : 'hidden'
						}, {
							xtype : 'label',
							text : '省'
						}, receiptAddress_prov_cmb, {
							xtype : 'label',
							text : '市'
						}, receiptAddress_city_cmb, {
							xtype : 'label',
							text : '区'
						}, receiptAddress_dist_cmb, {
							xtype : 'label',
							text : '邮编'
						}, {
							name : 'postCode',
							width : 210,
							xtype : 'textfield',
							regex : /^[0-9]+$/,
							regexText : '请输入阿拉伯数字格式',
							maxLength : "30",
							maxLengthText : "最大长度为30位"
						}, {
							xtype : 'label',
							text : '通讯地址'
						}, {
							name : 'street',
							xtype : 'textfield',
							preventScrollbars : true,
							anchor : "99%",
							maxLength : "60",
							maxLengthText : "最大长度为60位",
							grid : [2, 2, 8, 2]
						}, {
							xtype : 'label',
							text : '联系人'
						}, {
							name : 'receiverName',
							width : 210,
							maxLength : "30",
							maxLengthText : "最大长度为30位"
						}, {
							xtype : 'label',
							text : '电话'
						}, {
							name : 'phone',
							width : 210,
							xtype : 'textfield',
							maxLength : "30",
							maxLengthText : "最大长度为30位"
						}, {
							xtype : 'label',
							text : '手机'
						}, {
							name : 'mobile',
							xtype : 'textfield',
							width : 210,
							regex : /^[0-9]+$/,
							regexText : '请输入阿拉伯数字格式',
							maxLength : "30",
							maxLengthText : "最大长度为30位"
						}],
				buttons : [{
					text : '保存',
					iconCls : "icon-save",
					handler : function() {
						if (!receiptAddress_form.getForm().isValid()) {

							Ext.Msg.alert("提示", "请按要求进行正确的操作");

							return;

						}
						var formValues = receiptAddress_form.getForm()
								.getValues();
						var jsonAll = Ext.encode(formValues);

						receiptAddress_ajax.request({
							url : Uec3.App.Constants.homeUrl+ '/receiptAddress/edit_receiptAddress!save.do',
							success : function(res) {
								var result = Ext.decode(res.responseText);
								if (result.success == "OK") {

									var receiptAddress = result.receiptAddress;
									result.receiptAddress.address = '地址:'
											+ (receiptAddress.province
													? receiptAddress.province
													: '')
											+ (receiptAddress.city
													? receiptAddress.city
													: '')
											+ (receiptAddress.district
													? receiptAddress.district
													: '')
											+ (receiptAddress.street
													? receiptAddress.street
													: '')
											+ (receiptAddress.receiverName
													? ';联系人:'
															+ receiptAddress.receiverName
													: '')
											+ (receiptAddress.phone ? ';电话:'
													+ receiptAddress.phone : '')
											+ (receiptAddress.mobile
													? ';手机:'
															+ receiptAddress.mobile
													: '');
									scope.setLookupValue(result.receiptAddress,
											receiptAddress_form.ownerCt);
									var lookup = receiptAddress_form.ownerCt.lookup;
									if (lookup.lookupCallBackEvents) {
										Ext.each(lookup.lookupCallBackEvents,
												function(event) {
													if (event == 'lookupChange') {
														lookup.fireEvent(event,
																receiptAddress);
													} else {
														lookup.fireEvent(event);
													}
												});
									}
									receiptAddress_form.ownerCt.close();

								} else {
									Ext.Msg.alert("提示", result.failture);

								}

							},

							failure : function(res) {

								Ext.Msg.alert('失败', "网络出现异常，请重先登入");

							},

							params : {
								jsonAll : jsonAll
							}

						});

					}

				}, {
					text : '关闭',
					iconCls : "icon-close",
					handler : function() {

						receiptAddress_form.ownerCt.close();

					}

				}],

				buttonAlign : 'center'

			});

		};

		var controlReceiptAddressEvent = function() {

			receiptAddress_prov_cmb.on('select', function(combo) {

						var value = combo.getValue();

						receiptAddress_form.getForm().findField("areaId")
								.setValue(value);

					});

			receiptAddress_city_cmb.on('select', function(combo) {

						var value = combo.getValue();

						receiptAddress_form.getForm().findField("areaId")
								.setValue(value);

					});

			receiptAddress_dist_cmb.on('select', function(combo) {

						var value = combo.getValue();

						receiptAddress_form.getForm().findField("areaId")
								.setValue(value);

					});

			receiptAddress_ajax.on('beforerequest', function() {

						Ext.MessageBox.wait("保存数据中...", "提示");
					});

			receiptAddress_ajax.on('requestcomplete', function() {

						Ext.MessageBox.hide();
					});

		};

		receiptAddresscomponentInits();

		controlReceiptAddressEvent();

		return receiptAddress_form;

	},

	/**
	 * Create this modules window here.
	 */
	createWindow : function() {
		var desktop = this.app.getDesktop();
		var win = desktop.getWindow('receiptAddressAdd');
		if (!win) {
			win = desktop.createWindow({
						id : 'receiptAddressAdd',
						title : '收货地址增加',
						width : 650,
						iconCls : 'acc-icon',
						shim : false,
						modal : true,
						animCollapse : false,
						constrainHeader : true,
						maximizable : false,
						resizable : false,
						taskbuttonTooltip : "收货地址列表",
						layout : "fit",
						items : [this.getComponents(this)]
					});

		}
		win.show();

	}
});